/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.importer.transform;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.camel.Exchange;
import se.sll.service.catalogue.common.rest.request.VMIdMapCreateRequest;
import se.sll.service.catalogue.common.rest.response.HsaData;
import se.sll.service.catalogue.importer.extract.HsaIdVmIdProcessor;
import se.sll.service.catalogue.importer.extract.VMCareGiver;
import se.sll.service.catalogue.model.vm.Vantetid;

/**
 *
 * @author khaleddaham
 */
public class ToIdmap {

    public void process(Exchange exchange) throws IOException, FileNotFoundException, Exception {
        Map<String, Object> obj = (Map<String, Object>) exchange.getIn().getBody(Object.class);
        List<HsaData> hsaData = (List<HsaData>) obj.get("hsa");
        HsaIdVmIdProcessor hsaVmMap = new HsaIdVmIdProcessor();
        Set<VMIdMapCreateRequest> requests = new HashSet<>();
        for (Vantetid v : (List<Vantetid>) obj.get("times")) {
            VMIdMapCreateRequest vmIdMap = new VMIdMapCreateRequest();
            vmIdMap.setAllowImport(true);
            VMCareGiver vm = new VMCareGiver();
            vm.setId(v.getVardgivar().getVardgivarID());
            vm.setPartId(v.getVardgivarDel().getVardgivardelID());
            vmIdMap.setHsaId(hsaVmMap.getHSAIdForVMId(vm));
            vmIdMap.setEkId(vmIdMap.getHsaId());
            String name = "";
            for (HsaData h : hsaData) {
                if (h.getHsaId().equals(vmIdMap.getHsaId())) {
                    name = h.getRelativeDistinguishedName();
                }
            }
            vmIdMap.setRelativeDistinguishedName(name);
            vmIdMap.setVmCareGiverName(v.getVardgivar().getVardgivarNamn());
            vmIdMap.setVmCareGiverPartId(v.getVardgivarDel().getVardgivardelID());
            vmIdMap.setVmCareGiverPartName(v.getVardgivarDel().getVardgivardelNamn());
            vmIdMap.setVmId(v.getVardgivar().getVardgivarID());
            /**
             * System.out.println( v.getVardgivar().getVardgivarNamn() + ":" +
             * v.getVardgivar().getVardgivarID() + ", " +
             * v.getVardgivarDel().getVardgivardelNamn() + ":" +
             * v.getVardgivarDel().getVardgivardelID());
             */
            requests.add(vmIdMap);
        }
        exchange.getIn().setBody(requests);
    }
}
